{% extends "templates/atnet_base.html" %}

{% block csslink %}
		{{ super() }}
<script src="/media/js/jquery.js"></script>

{% include 'templates/includes_datatable.html' %}
{% include 'templates/includes_jquery_ui.html' %}
<script src="/media/js/form_popup.js"></script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
	var wasAction = false;

	$('#budget-sum').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": false,
		"bSort": false,
		"bInfo": false,
		"bAutoWidth": true,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='sum_json') }}",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			$('td:eq(2)', nRow).html('<nobr><a href="{{ uri_for('reports_web', imm_id=immeuble.key().id(), command='budget') }}' + aData[4] + '/" target="_blank">{{ _("Preview") }}</a> <a href="{{ uri_for('reports_web', imm_id=immeuble.key().id(), command='budget') }}' + aData[4] + '/?format=pdf" target="_blank">{{ _("Print") }}</a></nobr>');
		}
	});
	
	$('#budget-sum-regular').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": false,
		"bSort": false,
		"bInfo": false,
		"bAutoWidth": true,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='sum_json_regular') }}",
	});

	$('#budget-sum-additional').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": false,
		"bSort": false,
		"bInfo": false,
		"bAutoWidth": true,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='sum_json_additional') }}",
	});
	
	$('#budget-items-regular').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": true,
		"bSort": true,
		"bInfo": false,
		"bAutoWidth": false,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='list_json_regular') }}",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			$('td:eq(0)', nRow).html('<nobr>' + aData[0] + '</nobr>');
			$('td:eq(4)', nRow).html('<nobr><a href="javascript:void(0);" id="' + aData[4] + '" class="edit">{{ _("Edit") }}</a> <a href="javascript:void(0);" id="' + aData[4] + '" class="delete">{{ _("Delete") }}</a></nobr>');
		},
		 "aoColumnDefs": [
		                  { "sClass": "aright", "aTargets": [ 4 ] }
		                  ]
	});
	
	$('#add-bi').live('click', function(){
		$(this).LaunchPopup(
			{
				TableId: '#budget-items-regular,#budget-items-additional, #budget-sum-additional, #budget-sum-regular, #budget-sum',
				DivId: '#dialog-form',
				FormId: '#generated-form',
				UrlForm: "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='add') }}",
				Mode: 'add',
				Title: '{{ _("Add budget item") }}',
				UrlReturn: "{{ uri_for('creation_banque', imm_id=immeuble.key().id(), command='list') }}"
			}
		)
	});
	
	$('#budget-items-regular tbody tr a').live('click', function(e) {
		if ($(this).attr('class') == 'edit') {
			$(this).LaunchPopup(
					{
						TableId: '#budget-items-regular, #budget-sum-regular, #budget-sum',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='edit') }}" + e.target.id + "/",
						Mode: 'edit',
						Title: '{{ _("Edit budget item") }}'
					}
				)
		}
		if ($(this).attr('class') == 'delete') {
			$(this).LaunchPopup(
					{
						TableId: '#budget-items-regular, #budget-sum-regular, #budget-sum',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='delete') }}" + e.target.id + "/",
						Mode: 'delete',
						Title: '{{ _("Delete budget item") }}'
					}
				)
		}
	});
	
	$('#budget-items-additional').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": true,
		"bSort": true,
		"bInfo": false,
		"bAutoWidth": false,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='list_json_additional') }}",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			$('td:eq(0)', nRow).html('<nobr>' + aData[0] + '</nobr>');
			$('td:eq(4)', nRow).html('<nobr><a href="javascript:void(0);" id="' + aData[4] + '" class="edit">{{ _("Edit") }}</a> <a href="javascript:void(0);" id="' + aData[4] + '" class="delete">{{ _("Delete") }}</a></nobr>');
		},
		 "aoColumnDefs": [
		                  { "sClass": "aright", "aTargets": [ 4 ] }
		                  ]
	});
	
	$('#budget-items-additional tbody tr a').live('click', function(e) {
		if ($(this).attr('class') == 'edit') {
			$(this).LaunchPopup(
					{
						TableId: '#budget-items-additional, #budget-sum-additional, #budget-sum',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='edit') }}" + e.target.id + "/",
						Mode: 'edit',
						Title: '{{ _("Edit budget item") }}'
					}
				)
		}
		if ($(this).attr('class') == 'delete') {
			$(this).LaunchPopup(
					{
						TableId: '#budget-items-additional, #budget-sum-additional, #budget-sum',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('creation_budget', imm_id=immeuble.key().id(), command='delete') }}" + e.target.id + "/",
						Mode: 'delete',
						Title: '{{ _("Delete budget item") }}'
					}
				)
		}
	});
});
</script>
{% endblock %}

{% block title %} 
	{{ title }}
{% endblock %}

{% block header %}
{{ header }}
{% endblock %}

{% block login%}
{% if show_login %}
{{ super() }}
{% endif %}
{% endblock%}

{% block navigation %}
{% if not show_login %}
{{ super() }}
{% endif %}
{% endblock %}

{% block section_menu %}
{% include 'templates/includes_creation_menu.html' %}
{% endblock %}

{% block main_content %}
<p>
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="budget-sum"  width="50%">
		<thead>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th><nobr>{{ _(fields.summary_amount) }}</nobr></th>
				<th></th>
				<!--  <th><nobr>{{ _(fields.summary_amount_cash) }}</nobr></th> -->
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="2" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.summary_amount) }}</th>
				<th></th>
				<!-- <th>{{ _(fields.summary_amount_cash) }}</th> -->
			</tr>
		</tfoot>
	</table>
	<p><a href="javascript:void(0);" id="add-bi">{{ _('Add budget item') }}</a></p>
	<div id="demo">
	<fieldset>
		<legend>{{ _('Regular budget') }}</legend>
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="budget-items-regular" width="100%">
		<thead>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.expenditure) }}</th>
				<th>{{ _(fields.charges_key) }}</th>
				<th>{{ _(fields.amount) }}</th>
				<th width="20"></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="5" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.expenditure) }}</th>
				<th>{{ _(fields.charges_key) }}</th>
				<th>{{ _(fields.amount) }}</th>
				<th></th>
			</tr>
		</tfoot>
	</table>
	<br><br>
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="budget-sum-regular" width="50%">
		<thead>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th><nobr>{{ _(fields.amount_budget) }}</nobr></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="2" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.amount_budget) }}</th>
			</tr>
		</tfoot>
	</table>

	</fieldset>
	<p></p>
	<fieldset>
		<legend>{{ _('Additional budget') }}</legend>
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="budget-items-additional" width="100%">
		<thead>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.expenditure) }}</th>
				<th>{{ _(fields.charges_key) }}</th>
				<th>{{ _(fields.amount) }}</th>
				<th width="20"></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="5" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.expenditure) }}</th>
				<th>{{ _(fields.charges_key) }}</th>
				<th>{{ _(fields.amount) }}</th>
				<th></th>
			</tr>
		</tfoot>
	</table>

	<br><br>
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="budget-sum-additional" width="50%">
		<thead>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th><nobr>{{ _(fields.amount_budget) }}</nobr></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="2" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.period) }}</th>
				<th>{{ _(fields.amount_budget) }}</th>
			</tr>
		</tfoot>
	</table>
	
	</fieldset>
	</div>
	{% include 'templates/includes_popup_form.html' %}
</p>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
	var availableTagsCK;
	availableTagsCK = getMessages("{{ uri_for('parametrage_cles', imm_id=immeuble.key().id(), command='list_names_json') }}");
	$("#charges_key").live("focus", function() {
		$(this).autocomplete({
			delay: 0,
			minLength: 0,
			source: availableTagsCK
		});
		$(this).data("autocomplete").search($(this).val());
	});
	
	var availableTagsExp;
	availableTagsExp = getMessages("{{ uri_for('parametrage_nature_des_depenses', imm_id=immeuble.key().id(), command='list_names_json') }}");
	$("#expenditure").live("focus", function() {
		$(this).autocomplete({
			delay: 0,
			minLength: 0,
			source: availableTagsExp
		});
		$(this).data("autocomplete").search($(this).val());
	});
});
</script>
{% endblock %}
